package center

import (
	"fmt"
	"log"
	"net"
	"runtime"
	"strconv"
	"strings"
)

var dbcenter *DBcenter
var user_info *User_info

func Db_init(cfg *ServerConfig) {
	dbcenter = newDB(cfg)
	if dbcenter == nil {
		log.Fatal("cannot start mysql connections")
	}
}
func Load_gen_data_config() {
	rows, err := dbcenter.Read("select * from gen_config")
	if err != nil {
		fmt.Println("load gen config error")
		return
	}
	if rows == nil {
		fmt.Println("gen config no receord")
		return
	}
	for rows.Next() {
		gen_data := new(Gen_data)
		rows.Scan(&(gen_data.gen_id), &(gen_data.gen_name), &(gen_data.stand_frame_num), &(gen_data.stand_frame_interval), &(gen_data.run_frame_num), &(gen_data.run_frame_interval), &(gen_data.attack_frame_num), &(gen_data.attack_frame_interval), &(gen_data.attacked_frame_num), &(gen_data.attacked_frame_interval), &(gen_data.attack_val), &(gen_data.defensen_val))
		gen_config_map[gen_data.gen_id] = gen_data
		//fmt.Println("gen_data:", gen_data)
	}
	fmt.Println("len of gen_config:", len(gen_config_map))
	rows.Close()

}
func Load_prop_data_config() {
	rows, err := dbcenter.Read("select * from prop_config")
	if err != nil {
		fmt.Println("load_prop_data error")
		return
	}
	if rows == nil {
		fmt.Println("prop_data no receord")
		return
	}
	for rows.Next() {
		prop_data := new(Prop_data)
		rows.Scan(&prop_data.prop_id, &prop_data.prop_name, &prop_data.qulitly, &prop_data.can_overly, &prop_data.prop_type, &prop_data.price_silver, &prop_data.price_gold)
		prop_config_map[prop_data.prop_id] = prop_data
		//fmt.Println("prop_data:", prop_data)
	}
	fmt.Println("len of prop_config:", len(prop_config_map))
	rows.Close()
}
func Load_task_config() {
	rows, err := dbcenter.Read("select task_id,task_name,task_type from task_config")
	if err != nil {
		fmt.Println("load task_config error")
		return
	}
	if rows == nil {
		fmt.Println("load task_config no record")
		return
	}
	for rows.Next() {
		fmt.Println("task_config")
		task_data := new(Task_data)
		rows.Scan(&task_data.task_id, &task_data.task_name, &task_data.task_type)
		fmt.Println("task_data:", task_data) //注意，如果打印出来都是0值，有可能是sql 中的列数和row.Scan中的列数没对应好
		task_config_map[task_data.task_id] = task_data
		task_data.task_need_map = make(map[int32]*Task_need)
		task_data.task_reward_map = make(map[int32]*Task_reward)
	}
	fmt.Println("len of task_config_map:", len(task_config_map))
	rows.Close()
	//任务达成条件
	rows, err = dbcenter.Read("select * from task_need")
	if err != nil {
		fmt.Println("load task_need error")
		return
	}
	if rows == nil {
		fmt.Println("load task_need no record")
		return
	}
	for rows.Next() {
		task_need := new(Task_need)
		rows.Scan(&task_need.need_id, &task_need.task_id, &task_need.need_type, &task_need.need_val)
		task_data := task_config_map[task_need.task_id]
		fmt.Println("task_need:", task_need, task_need.need_id)
		task_data.task_need_map[task_need.need_id] = task_need
		fmt.Println("task_need:", task_need)
	}
	rows.Close()
	//任务奖励
	rows, err = dbcenter.Read("select * from task_reward")
	if err != nil {
		fmt.Println("load task_reward error")
		return
	}
	if rows == nil {
		fmt.Println("load task_reward no record")
		return
	}
	for rows.Next() {
		task_reward := new(Task_reward)
		rows.Scan(&task_reward.reward_id, &task_reward.task_id, &task_reward.reward_type, &task_reward.reward_num)
		task_data := task_config_map[task_reward.task_id]
		task_data.task_reward_map[task_reward.reward_id] = task_reward
		fmt.Println("task_reward: ", task_reward)
	}
	rows.Close()
}
func Load_fight_level_config() {
	//副本关卡配置
	rows, err := dbcenter.Read("select fight_level_id,lv_min,rewards from fight_level_config")
	if err != nil {
		fmt.Println("load_fight_level_config error")
		return
	}
	if rows == nil {
		fmt.Println("fight_level_config no receord")
		return
	}
	var rewards string
	for rows.Next() {
		fight_level_data := new(Fight_level_data)
		rows.Scan(&fight_level_data.fight_level_id, &fight_level_data.lv_min, &rewards)
		fight_level_map[fight_level_data.fight_level_id] = fight_level_data
		fight_level_data.monsterList = make(map[int32]*Gen_data)
		fight_level_data.rewardList = make(map[byte]*Level_Reward_data)
		//解析关卡奖励
		s := strings.Split(rewards, ",")
		num := byte(len(s) / 3)
		var index byte
		for {
			if index >= num {
				break
			}
			level_reward_data := new(Level_Reward_data)
			id, _ := strconv.Atoi(s[index*3])
			reward_id, _ := strconv.Atoi(s[index*3+1])
			rate, _ := strconv.Atoi(s[index*3+2])

			level_reward_data.reward_id = byte(id)
			level_reward_data.reward_num = int32(reward_id)
			level_reward_data.reward_rate = byte(rate)

			fight_level_data.rewardList[level_reward_data.reward_id] = level_reward_data
			index++
		}
		//fmt.Println("fight_level_data:", fight_level_data)
	}
	rows.Close()
	//副本关卡里面的Monster
	rows, err = dbcenter.Read("select * from fight_monster_config")
	if err != nil {
		fmt.Println("load_fight_monster_config error")
		return
	}
	if rows == nil {
		fmt.Println("fight_monster_config no receord")
		return
	}
	var monster_config_id int32
	var fight_level_id int32
	var gen_id int32
	for rows.Next() {

		rows.Scan(&monster_config_id, &fight_level_id, &gen_id)
		fight_level_data := fight_level_map[fight_level_id]
		if fight_level_data == nil {
			//fmt.Println("fight_level_data nil")
			continue
		}
		gen_data := gen_config_map[gen_id]
		if gen_data == nil {
			fmt.Println("monster_data nil")
			continue
		}
		fight_level_data.monsterList[monster_config_id] = gen_data
		fmt.Println("monster_data:", gen_data)
	}
	rows.Close()

}
func Load_user_base() {
	rows, err := dbcenter.Read("select * from user_info")
	if err != nil {
		fmt.Println("load_user_base error")
		return
	}
	if rows == nil {
		fmt.Println("user_base no receord")
		return
	}
	for rows.Next() {
		user_info = new(User_info)
		user_info.genlist = make(map[int16]*Gen_info)
		user_info.baglist = make(map[int32]*Bag_thing)
		user_info.tasklist = make(map[int32]*Task_user)
		rows.Scan(&(user_info.user_id), &(user_info.user_code), &(user_info.user_name), &(user_info.headid), &(user_info.country), &(user_info.city), &(user_info.scene_id), &(user_info.silver), &(user_info.gold), &(user_info.cd1), &(user_info.cd2), &(user_info.cd3), &(user_info.user_flag))
		user_info_map[user_info.user_id] = user_info
		fmt.Println("user:", user_info)
	}
	rows.Close()
}
func Load_user_task() {
	rows, err := dbcenter.Read("select * from task_user")
	if err != nil {
		fmt.Println("load_task_user error")
		return
	}
	if rows == nil {
		fmt.Println("task_user no receord")
		return
	}
	for rows.Next() {
		task_user := new(Task_user)
		rows.Scan(&(task_user.task_user_id), &(task_user.task_id), &(task_user.task_type), &(task_user.user_id), &(task_user.task_status))
		user_info := user_info_map[task_user.user_id]
		if user_info == nil {
			fmt.Println("task user not found!")
			continue
		}
		user_info.tasklist[task_user.task_user_id] = task_user
		fmt.Println("task_user:", task_user)
	}

	rows.Close()

}
func Load_user_gen() {
	rows, err := dbcenter.Read("select * from gen_info")
	if err != nil {
		fmt.Println("load user_gen error")
		return
	}
	if rows == nil {
		fmt.Println("gen_info no receord")
		return
	}
	for rows.Next() {
		gen_info := new(Gen_info)
		rows.Scan(&(gen_info.gen_info_id), &(gen_info.user_id), &(gen_info.lvone_id), &(gen_info.genlv), &(gen_info.attackval), &(gen_info.defensenval))
		//fmt.Println("user_gen:", gen_info)
		user_info := user_info_map[gen_info.user_id]
		if user_info == nil {
			//fmt.Println("user not found for gen!")
			continue
		}
		user_info.genlist[gen_info.lvone_id] = gen_info
	}
	rows.Close()
}
func Load_user_bagthings() {
	rows, err := dbcenter.Read("select * from bag_things")
	if err != nil {
		fmt.Println("load bag_thins error")
		return
	}
	if rows == nil {
		fmt.Println("bag_things no receord")
		return
	}
	for rows.Next() {
		bag_thing := new(Bag_thing)
		rows.Scan(&(bag_thing.bag_thing_id), &(bag_thing.user_id), &(bag_thing.prop_id), &(bag_thing.prop_lv), &(bag_thing.prop_num))
		user_info_map[bag_thing.user_id].baglist[bag_thing.bag_thing_id] = bag_thing
		fmt.Println("bag_thing:", bag_thing)
	}
	rows.Close()
}
func Load_country_info() {
	rows, err := dbcenter.Read("select * from country_info")
	if err != nil {
		fmt.Println("load country info error")
		return
	}
	if rows == nil {
		fmt.Println("country info no receord")
		return
	}
	for rows.Next() {
		country_info := new(Country_info)
		rows.Scan(&(country_info.country_id), &(country_info.user_id))
		country_map[country_info.country_id] = country_info
		fmt.Println("country_info:", country_info)
	}
	rows.Close()
}
func Load_city_info() {
	rows, err := dbcenter.Read("select * from city_info")
	if err != nil {
		fmt.Println("load city_info error")
		return
	}
	if rows == nil {
		fmt.Println("city info no receord")
		return
	}
	for rows.Next() {
		city_info := new(City_info)
		rows.Scan(&(city_info.city_id), &(city_info.country), &(city_info.user_id))
		country_map[city_info.country].city_list[city_info.city_id] = city_info
		fmt.Println("city_info:", city_info)
	}
	rows.Close()
}
func Init_Scene_list() {
	var i byte
	for i = 1; i <= 30; i++ {
		scene_info := new(Scene_info)
		scene_info.scene_id = i
		scene_info.user_list = make(map[int32]*User_info)
		scene_List[i] = scene_info
	}
}
func Listen_Serve(cfg *ServerConfig) {
	fmt.Println("num of CPU:", runtime.NumCPU())
	runtime.GOMAXPROCS(runtime.NumCPU())
	str_port := fmt.Sprintf(":%s", cfg.TCPPort)
	lis, err := net.Listen("tcp", str_port)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("Started... listening on tcp port %s\n", cfg.TCPPort)
	for {
		//等待连接
		var coni Conninfo
		var err error

		coni.Conn, err = lis.Accept()

		if err != nil {
			log.Fatal(err)
		}
		coni.intoroutine()
	}
}
